package com.tbvtc.agriculturalcreditchainserver2025.controller.admin;

import com.tbvtc.agriculturalcreditchainserver2025.exception.FoodException;
import com.tbvtc.agriculturalcreditchainserver2025.service.IpfsService;
import com.tbvtc.agriculturalcreditchainserver2025.service.impl.AuthServiceImpl;
import com.tbvtc.agriculturalcreditchainserver2025.utils.Result;
import com.tbvtc.agriculturalcreditchainserver2025.utils.RUtils;
import com.tbvtc.agriculturalcreditchainserver2025.utils.ResultCodeEnum;
import com.tbvtc.agriculturalcreditchainserver2025.vo.CaptchaVo;
import com.tbvtc.agriculturalcreditchainserver2025.vo.LoginDto;
import com.tbvtc.agriculturalcreditchainserver2025.vo.UserVo;
import com.tbvtc.agriculturalcreditchainserver2025.vo.farmerVo.loginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@CrossOrigin
@RestController
@Tag(name = "用户认证相关接口")
public class AuthController {

    @Autowired
    private AuthServiceImpl authService;
//    @PostMapping("/registerFarmer")

    @Autowired
    private IpfsService ipfsService;
    @Operation(summary = "注册接口")
    @PostMapping("/register")
    public Result register( @RequestBody UserVo userVo){
      log.info("注册");
      if (userVo.getPhone()==null){
          throw new FoodException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
      }
        authService.registerFarmer(userVo);
      return RUtils.success();
  }
  @PostMapping("/login")
    public Result login(  @RequestBody LoginDto loginDto){
      log.info("登录");
      loginVo loginVo = authService.LoginFarmer(loginDto);
      log.info("登录成功");
      return RUtils.success(loginVo);

  }
    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> captcha() {
        CaptchaVo result = authService.captcha();
        return RUtils.success(result);
    }
    @GetMapping("login/getCode")
    @Operation(summary = "获取短信验证码")
    public Result getCode(@RequestParam String phone) {
        authService.getCode(phone);
        return RUtils.success();
    }
    @PostMapping("login/findPassword")
    @Operation(summary = "找回密码")
    public Result findPassword(  String phone,String newPassword,String code) {
        authService.findPassword(phone,newPassword,code);
        return RUtils.success();
    }

}
